// __PUBLIC__/js/page/weiba/admin/index
require(['../../common'], function() {
  require(['admin.index'])
})

define('admin.index', [
  'jquery',
  'page/grid',
], function($, Grid) {
  'use strict';

  var $form = $('#J_form')
  var $table = $('.table')
  var mygrid = new Grid($table)
  mygrid.init();


  function recommend(id, act) {
    if (act == 'recommend') {
      var v = 'lang.recommend';
      var v2 = 'lang.cancel_recommend';
      var act2 = 'cancel';

    } else {
      var v = 'lang.cancel_recommend';
      var v2 = 'lang.recommend';
      var act2 = 'recommend';
    }
    if (confirm('lang.is' + v)) {
      $.post(APP + '?app=weiba&m=Admin&a=setRecommend', {
        weiba_id: id,
        act: act
      }, function(res) {
        console.log(res)
        if (res == '1') {
          alert('lang.operate_suc')
          $('#button' + id).html('<a href="javascript:void(0);" data-weibaid="' + id + '" data-act="' + act2 + '" id="J_rec">' + v2 + '</a>');
        } else {
          alert('lang.set_err')
        }
      });
    }
  }

  function deleteRecord(selected) {
    $.post(APP + '?app=weiba&m=Admin&a=delWeiba', {
      ids: selected.id.join()
    }).done(function(res) {
      if (res == '1') {
        mygrid.del(selected.index)
      } else {
        alert('lang.del_err')
      }
    });
  }

  $(document)
    .on('click', '#J_rec', function(e) {
      e.preventDefault()
      var weibaID = $(this).data('weibaid')
      var act = $(this).data('act')

      recommend(weibaID, act)
    })
    .on('click', '.A_del', function(e) {
      e.preventDefault()
      if (confirm('确定要解散微吧？')) {
        var current = mygrid.getCurrent(this)
        var selected = {
          id: [current.id],
          index: [current.index]
        }
        deleteRecord(selected)
      }
    })
    .on('click', '#J_del', function(e) {
      e.preventDefault()

      var selected = mygrid.getSelected()
      if (!selected.count) {
        alert('请选择一条数据')
      } else {
        if (confirm('确定要解散微吧？')) {
          deleteRecord(selected)
        }
      }
    })

})